// 导入模块
const fs = require('fs');
const path = require('path')

// 读取index.html
fs.readFile(path.join(__dirname, './素材/index.html'), 'utf-8', (err, datastr) => {
   err && console.log(err);
   // console.log(datastr);
   const cssStr = getCssStr(datastr)
   const jsStr = getJsStr(datastr)
   fs.writeFile(path.join(__dirname, './clock/index.css'), cssStr[1], 'utf-8', err => {
      err ? console.log(err) : console.log('写入成功')
   })
   fs.writeFile(path.join(__dirname, './clock/index.js'), jsStr[1], 'utf-8', err => {
      err ? console.log(err) : console.log('写入成功')
   })

   // 把style与script替换成外部链接
   const htmlStr = datastr
      .replace(cssStr[0], '<link rel="stylesheet" href="./index.css">')
      .replace(jsStr[0], '<script src="./index.js"></script>')
   // 写入
   fs.writeFile(path.join(__dirname, './clock/index.html'), htmlStr, 'utf-8', err => {
      err ? console.log(err) : console.log('写入成功')
   })
})
//封装提取css内容的方法
const getCssStr = data => {
   const reg = /<style>([\s\S]*)<\/style>/
   // console.log(reg.exec(data)[0])
   // console.log(reg.exec(data)[1])
   return reg.exec(data)
}
//封装提取js内容的方法
const getJsStr = data => {
   const reg = /<script>([\s\S]*)<\/script>/
   // console.log(reg.exec(data)[0])
   // console.log(reg.exec(data)[1])
   return reg.exec(data)
}

